﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LiveTranslatorWS.LiveTranslator;
using System.Xml;

namespace LiveTranslatorWS
{
    public static class LiveTranslatorWS
    {
/*
Arabic - Saudia Arabia ...1025 
Chinese - PRC ...2052 
Chinese - Taiwan ...1028 
Dutch ...1043 
English - United States ...1033 
French - Standard ...1036 
German - Standard ...1031 
Italian - Standard ...1040 
Japanese ...1041 
Korean ...1042 
Portuguese - Brazil ...1046 
Spanish - Standard ...1034 
*/

        private static string m_strQueryXMLTemplate =
            "<QueryPacket xmlns='urn:Microsoft.Search.Query' revision='1' build='(12.0.6213)' ><Query domain='{0297CD20-047F-4256-0900-000001000000}'><QueryId>{6CCEAAE2-B854-4127-9A4B-1140A71A190C}</QueryId><OriginatorId>{F6FF7BE0-F39C-4ddc-A7D0-09A4C6C647A5}</OriginatorId><SupportedFormats><Format revision='1'>urn:Microsoft.Search.Response.Content:Content</Format></SupportedFormats>" +
            "<Context>" +
            "<QueryText type='STRING' >[string to be translated]</QueryText>" +
            "</Context><Range id='result'></Range>" +
            "<OfficeContext xmlns='urn:Microsoft.Search.Query.Office.Context' revision='1'>" +
            "<ServiceData>[source LCID]/[target LCID]/1</ServiceData>" +
            "</OfficeContext></Query></QueryPacket>";

        public static string Translate(string text, string from_lcid, string to_lcid, out bool status)
        {
            status = false;
            if (from_lcid != null && to_lcid != null)
            {
                if (from_lcid != to_lcid && from_lcid != string.Empty && to_lcid != string.Empty)
                {

                    string result = String.Empty;
                    string provider = String.Empty;

                    LiveTranslator.OfficeQuery thisQuery = new LiveTranslator.OfficeQuery();

                    string body = m_strQueryXMLTemplate.Replace("[string to be translated]", text);
                    body = body.Replace("[source LCID]", from_lcid);
                    body = body.Replace("[target LCID]", to_lcid);


                    try
                    {
                        string xmlResponse = thisQuery.Query(body);

                        // Create an XmlNamespaceManager to resolve namespaces.
                        NameTable nt = new NameTable();
                        XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
                        nsmgr.PushScope();
                        nsmgr.AddNamespace("c", "urn:Microsoft.Search.Response.Content");
                        nsmgr.AddNamespace("df", "urn:Microsoft.Search.Response");  //default namespace

                        XmlDocument thisXml = new XmlDocument(nt);
                        thisXml.LoadXml(xmlResponse);

                        // check the status
                        XmlNode statusNode = thisXml.SelectSingleNode("//df:Status", nsmgr);
                        if (statusNode.InnerText.Equals("Success", StringComparison.InvariantCultureIgnoreCase))
                        {
                            // format response as HTML;
                            result = String.Empty;

                            // process the response paragraphs
                            foreach (XmlNode thisNode in thisXml.SelectNodes("//c:P/c:Char", nsmgr))
                            {
                                result += String.Format("{0}", thisNode.InnerText);
                            }

                            // get the provider
                            provider = thisXml.SelectSingleNode("//c:Line", nsmgr).InnerText;
                            status = true;
                        }
                        else
                        {
                            //result = "Translation Failed";
                            status = false;
                        }
                    }
                    catch (Exception exc)
                    {
                        //result = exc.Message;
                        status = false;
                    }

                    text = result;
                }
                else
                    status = true;
            }
            else
                status = true;

            return text;

        }

    }
}
